﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4CheckRR70x : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4CheckRR70x()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.RR70x.DoubleClick += RR70x_DoubleClick;
            this.RR70x.PopupMenuShowing += RR70x_PopupMenuShowing;
            this.f4RR70xContextMenu.ItemClicked += f4RR70xContextMenu_ItemClicked;
        }

        /////////////////////////////
        /// Call Edit Form
        /////////////////////////////
        private void callEditForm()
        {
            DataRow row = RR70x.GetFocusedDataRow();
            frm_Edit_F4CheckRR70x editForm = new frm_Edit_F4CheckRR70x();
            editForm.serial = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                //LoadData();
            }
            else
            {
                MessageBox.Show("Cannot edit selected row.");
            }
        }

        void f4RR70xContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4RR70xContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F4CheckRR70x add_Form = new frm_Add_F4CheckRR70x();
                    add_Form.ShowDialog();
                    //LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void RR70x_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4RR70xContextMenu.Show(MousePosition);
        }

        private void RR70x_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();                        
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT * FROM [231_UnitRR70x]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridRR70x.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("Written", typeof(string));
                newTable.Columns.Add("Date", typeof(string));
                newTable.Columns.Add("Checked", typeof(string));
                newTable.Columns.Add("Wiring Daiagram", typeof(string));
                newTable.Columns.Add("A1/R RC234", typeof(string));
                newTable.Columns.Add("A1/R RC234 Ver", typeof(string));
                newTable.Columns.Add("A1/R RC234 No", typeof(string));
                newTable.Columns.Add("A2/Z RC234", typeof(string));
                newTable.Columns.Add("A2/Z RC234 Ver", typeof(string));
                newTable.Columns.Add("A2/Z RC234 No", typeof(string));
                newTable.Columns.Add("A1 RD023", typeof(string));
                newTable.Columns.Add("A1 run", typeof(string));
                newTable.Columns.Add("A1 stop", typeof(string));
                newTable.Columns.Add("A2 RD023", typeof(string));
                newTable.Columns.Add("A2 run", typeof(string));
                newTable.Columns.Add("A2 stop", typeof(string));
                newTable.Columns.Add("Rot RD023", typeof(string));
                newTable.Columns.Add("Rot run", typeof(string));
                newTable.Columns.Add("Rot stop", typeof(string));
                newTable.Columns.Add("Z RD02x", typeof(string));
                newTable.Columns.Add("Z run", typeof(string));
                newTable.Columns.Add("Z stop", typeof(string));
                newTable.Columns.Add("A1-OH", typeof(string));
                newTable.Columns.Add("A1-OL", typeof(string));
                newTable.Columns.Add("A1-Q", typeof(string));
                newTable.Columns.Add("A1-OX", typeof(string));
                newTable.Columns.Add("A1-OC", typeof(string));
                newTable.Columns.Add("A1-OS", typeof(string));
                newTable.Columns.Add("A1-0S", typeof(string));
                newTable.Columns.Add("A2-OH", typeof(string));
                newTable.Columns.Add("A2-OL", typeof(string));
                newTable.Columns.Add("A2-Q", typeof(string));
                newTable.Columns.Add("A2-OX", typeof(string));
                newTable.Columns.Add("A2-OC", typeof(string));
                newTable.Columns.Add("A2-OS", typeof(string));
                newTable.Columns.Add("A2-0S", typeof(string));
                newTable.Columns.Add("R-OH", typeof(string));
                newTable.Columns.Add("R-OL", typeof(string));
                newTable.Columns.Add("R-Q", typeof(string));
                newTable.Columns.Add("R-OX", typeof(string));
                newTable.Columns.Add("R-OC", typeof(string));
                newTable.Columns.Add("R-OS", typeof(string));
                newTable.Columns.Add("R-0S", typeof(string));
                newTable.Columns.Add("Z-OH", typeof(string));
                newTable.Columns.Add("Z-OL", typeof(string));
                newTable.Columns.Add("Z-Q", typeof(string));
                newTable.Columns.Add("Z-OX", typeof(string));
                newTable.Columns.Add("Z-OC", typeof(string));
                newTable.Columns.Add("Z-OS", typeof(string));
                newTable.Columns.Add("Z-0S", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));
                gridRR70x.DataSource = newTable;
            }
            RR70x.Columns[0].Caption = "Serial No";
            RR70x.Columns[1].Caption = "Written";
            RR70x.Columns[2].Caption = "Date";
            RR70x.Columns[3].Caption = "Checked";
            RR70x.Columns[4].Caption = "Wiring Daiagram";
            RR70x.Columns[5].Caption = "A1/R RC234";
            RR70x.Columns[6].Caption = "A1/R RC234 Ver";
            RR70x.Columns[7].Caption = "A1/R RC234 No";
            RR70x.Columns[8].Caption = "A2/Z RC234";
            RR70x.Columns[9].Caption = "A2/Z RC234 Ver";
            RR70x.Columns[10].Caption = "A2/Z RC234 No";
            RR70x.Columns[11].Caption = "A1 RD023";
            RR70x.Columns[12].Caption = "A1 run";
            RR70x.Columns[13].Caption = "A1 stop";
            RR70x.Columns[14].Caption = "A2 RD023";
            RR70x.Columns[15].Caption = "A2 run";
            RR70x.Columns[16].Caption = "A2 stop";
            RR70x.Columns[17].Caption = "Rot RD023";
            RR70x.Columns[18].Caption = "Rot run";
            RR70x.Columns[19].Caption = "Rot stop";
            RR70x.Columns[20].Caption = "Z RD02x";
            RR70x.Columns[21].Caption = "Z run";
            RR70x.Columns[22].Caption = "Z stop";
            RR70x.Columns[23].Caption = "A1-OH";
            RR70x.Columns[24].Caption = "A1-OL";
            RR70x.Columns[25].Caption = "A1-Q";
            RR70x.Columns[26].Caption = "A1-OX";
            RR70x.Columns[27].Caption = "A1-OC";
            RR70x.Columns[28].Caption = "A1-OS";
            RR70x.Columns[29].Caption = "A1-0S";
            RR70x.Columns[30].Caption = "A2-OH";
            RR70x.Columns[31].Caption = "A2-OL";
            RR70x.Columns[32].Caption = "A2-Q";
            RR70x.Columns[33].Caption = "A2-OX";
            RR70x.Columns[34].Caption = "A2-OC";
            RR70x.Columns[35].Caption = "A2-OS";
            RR70x.Columns[36].Caption = "A2-0S";
            RR70x.Columns[37].Caption = "R-OH";
            RR70x.Columns[38].Caption = "R-OL";
            RR70x.Columns[39].Caption = "R-Q";
            RR70x.Columns[40].Caption = "R-OX";
            RR70x.Columns[41].Caption = "R-OC";
            RR70x.Columns[42].Caption = "R-OS";
            RR70x.Columns[43].Caption = "R-0S";
            RR70x.Columns[44].Caption = "Z-OH";
            RR70x.Columns[45].Caption = "Z-OL";
            RR70x.Columns[46].Caption = "Z-Q";
            RR70x.Columns[47].Caption = "Z-OX";
            RR70x.Columns[48].Caption = "Z-OC";
            RR70x.Columns[49].Caption = "Z-OS";
            RR70x.Columns[50].Caption = "Z-0S";
            RR70x.Columns[51].Caption = "Remark";
            RR70x.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F4CheckRR70x add_Form = new frm_Add_F4CheckRR70x();
            add_Form.ShowDialog();
            //LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
